<template xmlns="http://www.w3.org/1999/html">
  <div class="app-container">
    <el-card>
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
        <el-form-item  prop="couponName">
          <el-input
            v-model="queryParams.couponName"
            placeholder="请输入优惠券名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item  prop="getType">
          <el-select
            v-model="queryParams.getType"
            placeholder="领取方式"
            clearable
            style="width: 240px"
          >
            <el-option
              v-for="dict in dict.type.mall_coupon_get_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item  prop="state">
          <el-select
            v-model="queryParams.state"
            placeholder="状态"
            clearable
            style="width: 240px"
          >
            <el-option
              v-for="dict in dict.type.mall_promotion_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item >
          <el-date-picker
            v-model="dateRange"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="活动开始日期"
            end-placeholder="活动结束日期"
          ></el-date-picker>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-table v-loading="loading" :data="couponList" >
        <el-table-column label="选中"  align="center" prop="selected" >
          <template slot-scope="scope">
            <i class="el-icon-circle-check" v-if="scope.row.selected" style="font-size: 20px;color: red"></i>
          </template>
        </el-table-column>
        <el-table-column label="优惠券名称" align="center" prop="couponName" />
        <el-table-column label="活动类型" align="center" prop="couponType" >
          <template slot-scope="scope">
            <dict-tag :options="dict.type.mall_coupon_type" :value="scope.row.couponType"/>
          </template>
        </el-table-column>
        <el-table-column label="面额/折扣" align="center" >
          <template slot-scope="scope">
            <span v-if="scope.row.couponType == 'PRICE'">{{ scope.row.price }} / 元</span>
            <span v-if="scope.row.couponType == 'POINT'">{{ scope.row.couponDiscount }} / 折</span>
          </template>
        </el-table-column>

        <el-table-column label="已领取/总数量/领取限制" align="center" prop="receivedNum" >
          <template slot-scope="scope">
            {{ scope.row.receivedNum }} /
            <span >{{ scope.row.publishNum == -1 ? '不限制' :  scope.row.publishNum}}</span> /
            <span >{{ scope.row.couponLimitNum == -1 ? '不限制' :  scope.row.couponLimitNum}}</span>
          </template>
        </el-table-column>
        <el-table-column label="已使用/已领取" align="center" prop="receivedNum" >
          <template slot-scope="scope">
            {{ scope.row.usedNum }} /
            {{ scope.row.receivedNum}}
          </template>
        </el-table-column>
        <el-table-column label="优惠券类型" align="center" prop="getType" >
          <template slot-scope="scope">
            <dict-tag :options="dict.type.mall_coupon_get_type" :value="scope.row.getType"/>
          </template>
        </el-table-column>

        <el-table-column label="品类描述" align="center" prop="scopeType" >
          <template slot-scope="scope">
            <dict-tag :options="dict.type.mall_coupon_scope_type" :value="scope.row.scopeType"/>
          </template>
        </el-table-column>
        <el-table-column label="活动时间" align="center" prop="endTime" width="180">
          <template slot-scope="scope" >
            <div v-if="scope.row.getType === 'FREE'">
              <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span> </br>
              <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
            </div>
            <span v-else> 长期有效</span>

          </template>
        </el-table-column>

        <el-table-column label="操作" align="center" class-name="small-padding fixed-width"  fixed="right" width="80">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-check"
              v-if="!scope.row.selected"
              @click="handChecke(scope.row)"
            >选中</el-button>

            <el-button
              size="mini"
              type="text"
              icon="el-icon-close"
              v-if="scope.row.selected && type == 'multiple'"
              @click="handChecke(scope.row)"
            >取消</el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <div slot="footer" style="text-align: right;margin-top: 15px">
      <el-button type="primary" @click="handleChoose">确 定</el-button>
    </div>
  </div>
</template>

<script>
import {
  listCoupon,
} from "@/api/mall/promotion/coupon";
export default {
  name: "CouponChoose",
  dicts: ['mall_coupon_get_type','mall_coupon_type','mall_coupon_scope_type','mall_promotion_state'],
  props: {
    selectedWay: {
      type: Array,
      default: function () {
        return new Array();
      },
    },
    chooseType: {
      type: String,
      default : 'multiple'
    }
  },
  data() {
    return {
      type: this.chooseType, //单选或者多选 single  multiple
      selectedData: this.selectedWay,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商城优惠券表格数据
      couponList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      formDateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        isEnable: 1,
        endTime: null,
        promotionName: null,
        storeId: null,
        storeName: null,
        startTime: null,
        couponName: null,
        couponType: null,
        getType: null,
        state: null,
        scopeType: null,
      },
      // 表单参数
      form: {},
      rangeTimeType: 1,
      id: null
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询商城优惠券列表 */
    getList() {
      this.loading = true;
      if (this.dateRange.length > 0){
        this.queryParams.startTime = this.dateRange[0]
        this.queryParams.endTime = this.dateRange[1]
      }
      let _this = this
      listCoupon(this.queryParams).then(response => {

        response.rows.forEach((item) => {
          item.selected = false;
          _this.selectedWay.forEach((e) => {
            if (e.id && e.id === item.id) {
              item.selected = true;
            }
          });
        });

        this.couponList = response.rows;


        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 选中按钮操作 */
    handleChoose() {
      this.$emit("selectedCouponData", this.selectedData);
    },
    /**
     * 选中事件
     */
    handChecke(row) {
      // 如果单选的话
      if (this.type != "multiple") {
        this.couponList.forEach((item) => {
          item.selected = false;
        });
        this.selectedData = [];
        row.selected = true;
        this.selectedData.push(row);

        return false;
      }

      if (row.selected == false) {
        row.selected = true;
        row.num = 1
        this.selectedData.push(row);
      } else {
        row.selected = false;
        for (let i = 0; i < this.selectedData.length; i++) {
          if (this.selectedData[i].id === row.id) {
            this.selectedData.splice(i, 1);
            break;
          }
        }
      }
    },
  }
};
</script>
